home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / hf / dsp / source / hdlc.asm < prev    next >
Encoding:
Assembly Source File  |  1991-08-31  |  6.7 KB  |  277 lines

  1.     page    132,63,1,1
  2.     opt    rc
  3.     title    'HDLC protocol handling'
  4.  
  5. ;***************************************************************
  6. ;* HDLC.ASM -- HDLC protocol handling                   *
  7. ;*                                   *
  8. ;* Provides a HDLC low-level protocol handling:            *
  9. ;* flag generation/detecting, CRC calculation/checking,        *
  10. ;* zero insertion/deletion and data serializer.            *
  11. ;*                                   *
  12. ;* HDLC protocol handling is based on article               *
  13. ;*    Carlson, D., E.:                       *
  14. ;*    "Bit-Oriented Data Link Control Procedures",           *
  15. ;*    IEEE Trans. on Comm., Vol. 28, No. 4, April 1980       *
  16. ;* CRC calculation/checking is based on article            *
  17. ;*    Morse, G.:                           *
  18. ;*    "Calculating CRCs by bits and bytes",               *
  19. ;*    BYTE Vol. 11, No. 9, September 1986               *
  20. ;*                                   *
  21. ;* This module uses registers as follows:               *
  22. ;*  r0 - general purpose                       *
  23. ;*                                   *
  24. ;* Copyright (C) 1990, 1991 by Alef Null. All rights reserved. *
  25. ;* Author(s): Jarkko Vuori, OH2LNS                   *
  26. ;* Modification(s):                           *
  27. ;***************************************************************
  28.  
  29.     section HDLC
  30.     xref    rdhost,tsthost
  31.     xref    wrhost,endfrm,rejfrm
  32.  
  33.     xdef    HDLC_i
  34.     xdef    getbit,putbit
  35.  
  36. ; HDLC equations
  37. flagmsk equ    $ff                    ; left justified flag mask
  38. flag    equ    %01111110                ; HDLC bit flag
  39. abrtmsk equ    $fe                    ; left justified abort mask
  40. abort    equ    %11111110                ; abort sequence
  41. fivemsk equ    $fe                    ; left justified five bit mask
  42. five    equ    %01111100                ; left justified five bit sequence
  43. poly    equ    $8408                    ; HDLC CRC polynomial (x^16 + x^12 + x^5 + 1)
  44. crcchk    equ    $f0b8                    ; special CRC checkword
  45.  
  46. ; Xmitter status flags
  47. ztstflg equ    0
  48. zinsflg equ    1
  49.  
  50. ; Receiver status flags
  51. hunt    equ    0
  52. firstb    equ    1
  53. scndb    equ    2
  54.  
  55.     org    p:
  56.  
  57.     nolist
  58.     include 'macros'
  59.     list
  60.  
  61. ; CRC calculation routine
  62. ; data in the LSB of a
  63. crc    macro    rem
  64.     move            #>1,x0
  65.     and    x0,a        y:<rem,x0
  66.     eor    x0,a
  67.     lsr    a        #>poly,x0
  68.     jcc    <_crc1
  69.     eor    x0,a
  70. _crc1    move            a1,y:<rem
  71.     endm
  72.  
  73.  
  74. ;****************************
  75. ;* Initialize HDLC handling *
  76. ;****************************
  77. HDLC_i
  78. ; initialize coder
  79.     movi    xstD,y:<xstate
  80.     movi    flag,y:<xdata
  81.     movi    $0,y:<x5bit
  82.     movi    0,y:<xbit
  83.     movi    $0,y:<xstatus                ; disable 11111 check
  84. ; initialize decoder
  85.     movi    0,y:<rdata
  86.     movi    $0,y:<rflag
  87.     movi    $7,y:<rstatus                ; hunt mode
  88.  
  89.     rts
  90.  
  91.  
  92. ;****************************
  93. ;*      Get a bit        *
  94. ;****************************
  95. ; returns next bit to be sent in C
  96. ; returns Z if this is an end of the transmission
  97. ; Note! Interrupts are disabled if end of transmission detected
  98. getbit
  99. ; check if we must insert a zero
  100.     bclr    #zinsflg,y:<xstatus
  101.     jcs    <getins
  102. ; check if there are bits left
  103.     move            y:<xbit,a
  104.     tst    a        y:<xstate,r0
  105.     nop
  106.     jeq    (r0)
  107. ; five bit sequence detection logic
  108. getsft0 jclr    #ztstflg,y:<xstatus,getsft1        ; check if logic enabled
  109.     move            y:<xdata,a
  110.     lsr    a
  111.     move            y:<x5bit,a
  112.     ror    a        #>$f80000,x0
  113.     and    x0,a
  114.     cmp    x0,a        a1,y:<x5bit
  115.     jne    <getsft1
  116. ; 11111 detected, insert zero
  117.     bset    #zinsflg,y:<xstatus
  118. ; calculate CRC
  119. getsft1 move            y:<xdata,a
  120.     crc    xcrcrem
  121. ; shift data out (LSB first) and decrement bit counter
  122.     move            y:<xbit,r0
  123.     move            y:<xdata,a
  124.     lsr    a        (r0)-
  125.     move            r0,y:<xbit
  126.     move            a1,y:<xdata
  127.     andi    #$fb,ccr                ; NZ
  128.     rts
  129. ; insert zero bit
  130. getins    clr    a                    ; reset five bit counter
  131.     move            a1,y:<x5bit
  132.     andi    #$fa,ccr                ; NC NZ
  133.     rts
  134.  
  135.  
  136. ; --- A, after a begin flag
  137. xstA
  138. ; set up data xmission
  139.     movi    xstB,y:<xstate
  140.     movi    $00ffff,y:<xcrcrem            ; init CRC generator
  141.     movi    $1,y:<xstatus                ; enable 11111 checker
  142.  
  143. ; --- B, after data byte sent
  144. xstB    movi    8,y:<xbit                ; init bit counter for the next byte
  145.     jsr    <rdhost                 ; fetch next byte
  146.     move    x0,y:<xdata
  147.     jpl    <getsft0
  148. ; last databyte sent, send CRC
  149.     movi    xstC,y:<xstate
  150.     movi    16,y:<xbit
  151.     move            y:<xcrcrem,a
  152.     not    a
  153.     move            a1,y:<xdata
  154.     jmp    <getsft0
  155.  
  156. ; --- C, after CRC sent
  157. xstC    movi    xstD,y:<xstate
  158.     movi    flag,y:<xdata
  159.     movi    8,y:<xbit
  160.     movi    $0,y:<xstatus                ; disable 11111 checker
  161.     jmp    <getsft0
  162.  
  163. ; --- D, after the last flag sent
  164. xstD    ori    #$02,mr
  165.     jsr    <tsthost
  166.     jeq    <xstD1
  167. ; new data to send, start a new frame
  168.     andi    #$fc,mr
  169.     movi    xstA,y:<xstate
  170.     movi    flag,y:<xdata
  171.     movi    8,y:<xbit
  172.     jmp    <getsft0
  173.     rts
  174. ; no new data, return with Z
  175. xstD1    rts
  176.  
  177.  
  178. ;****************************
  179. ;*      Put a bit        *
  180. ;****************************
  181. ; put next bit in C to the host transmit queue
  182. putbit
  183.     move            y:<rflag,a
  184.     ror    a        #abrtmsk,b
  185.     move            a1,y:<rflag
  186.     move            a1,x0
  187. ; check if abort sequence detected
  188.     and    x0,b        #abort,y0
  189.     eor    y0,b
  190.     jeq    <putb4
  191. ; check if flag detected
  192.     move            #flagmsk,b
  193.     and    x0,b        #flag,y0
  194.     eor    y0,b        #fivemsk,a
  195.     jeq    <putb3                    ; yes, special handling
  196. ; check if 11111 sequence detected
  197.     and    x0,a        #five,y0
  198.     eor    y0,a        x0,b
  199.     jeq    <putb2                    ; yes, ignore this bit
  200. ; no special sequence detected, shift data normally
  201.     jset    #hunt,y:<rstatus,putb2
  202.     lsl    b        y:<rdata,a
  203.     ror    a        #>@pow(2,-15),x1
  204.     move            a1,y:<rdata
  205.     move            a1,x0
  206. ; calculate CRC
  207.     mpy    x0,x1,a     #>1,x1            ; shift to right 15 bits
  208.     crc    rcrcrem
  209. ; decrement the bit counter
  210.     move            y:<rbit,a
  211.     sub    x1,a        #8,r0
  212.     move            a,y:<rbit
  213.     jne    <putb2
  214. ; 8 bit shifted, init bit counter again
  215.     move            r0,y:<rbit
  216.     bclr    #firstb,y:<rstatus
  217.     jcc    <putb1
  218. ; first byte, init CRC checker
  219.     move            #$00ffff,a1
  220.     move            a1,y:<rcrcrem
  221.     rts
  222. ; data bytes, put it to the queue
  223. putb1    bclr    #scndb,y:<rstatus
  224.     jcs    <putb2
  225.     move            y:<rdata,a
  226.     move            #>$ff,x0
  227.     and    x0,a
  228.     move            a1,x0
  229.     jmp    <wrhost
  230. ; discard the previous bit
  231. putb2    rts
  232. ; flag detected
  233. putb3    bclr    #hunt,y:<rstatus
  234.     movi    8,y:<rbit
  235.     bset    #firstb,y:<rstatus
  236.     bset    #scndb,y:<rstatus
  237.     jcs    <rejfrm                 ; reject frame if it is too short
  238. ; calculate the last CRC bit
  239.     move            y:<rdata,x0
  240.     move            #>@pow(2,-16),x1        ; shift to right 16 bits
  241.     mpy    x0,x1,a
  242.     crc    rcrcrem
  243. ; check that it is valid
  244.     move            #>crcchk,x0
  245.     eor    x0,a
  246.     jeq    <endfrm
  247.     jmp    <rejfrm                 ; reject frame if CRC failed
  248. ; abort detected
  249. putb4    bset    #hunt,y:<rstatus
  250.     jmp    <rejfrm
  251.  
  252.  
  253. ;****************************
  254. ;*     DATA - AREA        *
  255. ;****************************
  256.  
  257.     org    y:
  258.  
  259. ; coder
  260. xstate    ds    1                    ; current xmitter state
  261. xdata    ds    1                    ; current byte to be send
  262. x5bit    ds    1                    ; one bit counter
  263. xstatus ds    1                    ; current xmitter status
  264. xbit    ds    1                    ; send bits counter
  265. xcrcrem ds    1                    ; CRC remainder
  266.  
  267. ; decoder
  268. rdata    ds    1                    ; current byte received
  269. rflag    ds    1                    ; one bit counter
  270. rstatus ds    1                    ; current receiver status
  271. rbit    ds    1                    ; received bit counter
  272. rcrcrem ds    1                    ; CRC remainder
  273.  
  274.     endsec
  275.  
  276.     end
  277.